//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction.xaframework
{

	using Xid = javax.transaction.xa.Xid;

	internal class MemoryMappedLogBuffer : LogBuffer
	{
		private const int MAPPED_SIZE = 1024 * 1024 * 2;

		private readonly FileChannel fileChannel;

		private MappedByteBuffer mappedBuffer = null;
		private long mappedStartPosition;
		private readonly ByteBuffer fallbackBuffer;

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: MemoryMappedLogBuffer(FileChannel fileChannel) throws IOException
		internal MemoryMappedLogBuffer(FileChannel fileChannel)
		{
			this.fileChannel = fileChannel;
			mappedStartPosition = fileChannel.position();
			getNewMappedBuffer();
			fallbackBuffer = ByteBuffer.allocateDirect(9 + Xid.MAXGTRIDSIZE + Xid.MAXBQUALSIZE * 10);
		}

		internal virtual MappedByteBuffer getMappedBuffer()
		{
			return mappedBuffer;
		}

		private int mapFail = -1;

		private void getNewMappedBuffer()
		{
			try
			{
				if (mappedBuffer != null)
				{
					mappedStartPosition += mappedBuffer.position();
					mappedBuffer.force();
					mappedBuffer = null;
				}
				if (mapFail > 1000)
				{
					mapFail = -1;
				}
				if (mapFail > 0)
				{
					mapFail++;
					return;
				}
				mappedBuffer = fileChannel.map(MapMode.READ_WRITE, mappedStartPosition, MAPPED_SIZE);
			}
			catch (System.Exception t)
			{
				mapFail = 1;
				t.printStackTrace();
			}
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(byte b) throws IOException
		public virtual LogBuffer put(sbyte b)
		{
			if (mappedBuffer == null || (MAPPED_SIZE - mappedBuffer.position()) < 1)
			{
				getNewMappedBuffer();
				if (mappedBuffer == null)
				{
					fallbackBuffer.Clear();
					fallbackBuffer.put(b);
					fallbackBuffer.flip();
					fileChannel.write(fallbackBuffer, mappedStartPosition);
					mappedStartPosition += 1;
					return this;
				}
			}
			mappedBuffer.put(b);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer putInt(int i) throws IOException
		public virtual LogBuffer putInt(int i)
		{
			if (mappedBuffer == null || (MAPPED_SIZE - mappedBuffer.position()) < 4)
			{
				getNewMappedBuffer();
				if (mappedBuffer == null)
				{
					fallbackBuffer.Clear();
					fallbackBuffer.putInt(i);
					fallbackBuffer.flip();
					fileChannel.write(fallbackBuffer, mappedStartPosition);
					mappedStartPosition += 4;
					return this;
				}
			}
			mappedBuffer.putInt(i);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer putLong(long l) throws IOException
		public virtual LogBuffer putLong(long l)
		{
			if (mappedBuffer == null || (MAPPED_SIZE - mappedBuffer.position()) < 8)
			{
				getNewMappedBuffer();
				if (mappedBuffer == null)
				{
					fallbackBuffer.Clear();
					fallbackBuffer.putLong(l);
					fallbackBuffer.flip();
					fileChannel.write(fallbackBuffer, mappedStartPosition);
					mappedStartPosition += 8;
					return this;
				}
			}
			mappedBuffer.putLong(l);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(byte[] bytes) throws IOException
		public virtual LogBuffer put(sbyte[] bytes)
		{
			if (mappedBuffer == null || (MAPPED_SIZE - mappedBuffer.position()) < bytes.Length)
			{
				getNewMappedBuffer();
				if (mappedBuffer == null)
				{
					fallbackBuffer.Clear();
					fallbackBuffer.put(bytes);
					fallbackBuffer.flip();
					fileChannel.write(fallbackBuffer, mappedStartPosition);
					mappedStartPosition += bytes.Length;
					return this;
				}
			}
			mappedBuffer.put(bytes);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(char[] chars) throws IOException
		public virtual LogBuffer put(char[] chars)
		{
			if (mappedBuffer == null || (MAPPED_SIZE - mappedBuffer.position()) < (chars.Length * 2))
			{
				getNewMappedBuffer();
				if (mappedBuffer == null)
				{
					fallbackBuffer.Clear();
					fallbackBuffer.asCharBuffer().put(chars);
					fallbackBuffer.limit(chars.Length * 2);
					fileChannel.write(fallbackBuffer, mappedStartPosition);
					mappedStartPosition += (chars.Length * 2);
					return this;
				}
			}
			int oldPos = mappedBuffer.position();
			mappedBuffer.asCharBuffer().put(chars);
			mappedBuffer.position(oldPos + chars.Length * 2);
			return this;
		}

		internal virtual void releaseMemoryMapped()
		{
			if (mappedBuffer != null)
			{
				mappedBuffer.force();
				mappedBuffer = null;
			}
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void force() throws IOException
		public virtual void force()
		{
			if (mappedBuffer != null)
			{
				mappedBuffer.force();
			}
			fileChannel.force(false);
		}

		public virtual long getFileChannelPosition()
		{
			if (mappedBuffer != null)
			{
				return mappedStartPosition + mappedBuffer.position();
			}
			return mappedStartPosition;
		}

		public virtual FileChannel getFileChannel()
		{
			return fileChannel;
		}
	}
}